package com.quad.innovators.salesease.service.impl.stock;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quad.innovators.salesease.common.result.PageResultBean;
import com.quad.innovators.salesease.converter.ProductInfoIdentityConverter;
import com.quad.innovators.salesease.enums.ScanCodeTypeEnum;
import com.quad.innovators.salesease.mapper.stock.ProductInfoIdentityMapper;
import com.quad.innovators.salesease.model.dto.ProductArrivalDetailNumber;
import com.quad.innovators.salesease.model.entity.stock.ProductInfoIdentity;
import com.quad.innovators.salesease.model.form.base.ProductForm;
import com.quad.innovators.salesease.model.query.stock.ProductInfoIdentityPageGroupQuery;
import com.quad.innovators.salesease.model.query.stock.ProductInfoIdentityPageQuery;
import com.quad.innovators.salesease.model.vo.stock.ProductInfoDetailVO;
import com.quad.innovators.salesease.model.vo.stock.ProductInfoIdentityGroupVO;
import com.quad.innovators.salesease.model.vo.stock.ProductInfoIdentityVO;
import com.quad.innovators.salesease.service.base.ProductService;
import com.quad.innovators.salesease.service.stock.ProductInfoIdentityService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author : shanshan.li
 * @createTime : 2025/3/28 11:07
 * @Version : 1.0 
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ProductInfoIdentityServiceImpl extends ServiceImpl<ProductInfoIdentityMapper, ProductInfoIdentity> implements ProductInfoIdentityService {

    private final ProductInfoIdentityConverter productInfoIdentityConverter;

    private final ProductService productService;

    @Override
    public PageResultBean<ProductInfoIdentityVO> listPage(ProductInfoIdentityPageQuery queryParams) {
        Page<ProductInfoIdentityVO> page = new Page<>(queryParams.getPageNum(), queryParams.getPageSize());
        Page<ProductInfoIdentityVO> productInfoIdentityPage = this.baseMapper.listPage(page, queryParams);
        return productInfoIdentityConverter.toPageVo(productInfoIdentityPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchInsert(List<ProductInfoIdentity> productInfoIdentityList) {
        return this.saveBatch(productInfoIdentityList);
    }

    @Override
    public List<ProductInfoIdentityGroupVO> listPageGroup(ProductInfoIdentityPageGroupQuery queryParams) {
        return this.baseMapper.listPageGroup(queryParams);
    }

    @Override
    public ProductInfoDetailVO getPackIngOrIdentityCode(String packingOrIdentityCode) {
        List<ProductInfoIdentity> productInfoIdentity = this.list(new QueryWrapper<ProductInfoIdentity>().lambda().eq(ProductInfoIdentity::getPackingCode, packingOrIdentityCode));
        ProductInfoDetailVO productInfoDetailVO = new ProductInfoDetailVO();
        productInfoDetailVO.setScanCodeType(ScanCodeTypeEnum.PACKING_CODE.getCode());
        if (CollectionUtils.isEmpty(productInfoIdentity)) {
            productInfoIdentity = this.list(new QueryWrapper<ProductInfoIdentity>().lambda().eq(ProductInfoIdentity::getIdentityCode, packingOrIdentityCode));
            productInfoDetailVO.setScanCodeType(ScanCodeTypeEnum.IDENTITY_CODE.getCode());
        }
        if (CollectionUtils.isEmpty(productInfoIdentity)) {
            return null;
        }
        ProductForm formData = productService.getFormData(productInfoIdentity.get(0).getProductCode());
        productInfoDetailVO.setProductCode(formData.getCode());
        productInfoDetailVO.setProductName(formData.getName());
        productInfoDetailVO.setProductNumber(formData.getPackingNumber());
        return productInfoDetailVO;
    }
}
